]> git.saurik.com Git - apple/libdispatch.git/blob - examples/Dispatch Samples/readFileF.c
libdispatch-84.5.5.tar.gz
[apple/libdispatch.git] / examples / Dispatch Samples / readFileF.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_DTS_LICENSE_HEADER_START@
5 *
6 * IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
7 * ("Apple") in consideration of your agreement to the following terms, and your
8 * use, installation, modification or redistribution of this Apple software
9 * constitutes acceptance of these terms. If you do not agree with these terms,
10 * please do not use, install, modify or redistribute this Apple software.
11 *
12 * In consideration of your agreement to abide by the following terms, and
13 * subject to these terms, Apple grants you a personal, non-exclusive license,
14 * under Apple's copyrights in this original Apple software (the "Apple Software"),
15 * to use, reproduce, modify and redistribute the Apple Software, with or without
16 * modifications, in source and/or binary forms; provided that if you redistribute
17 * the Apple Software in its entirety and without modifications, you must retain
18 * this notice and the following text and disclaimers in all such redistributions
19 * of the Apple Software. Neither the name, trademarks, service marks or logos of
20 * Apple Computer, Inc. may be used to endorse or promote products derived from
21 * the Apple Software without specific prior written permission from Apple. Except
22 * as expressly stated in this notice, no other rights or licenses, express or
23 * implied, are granted by Apple herein, including but not limited to any patent
24 * rights that may be infringed by your derivative works or by other works in
25 * which the Apple Software may be incorporated.
26 *
27 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
28 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
29 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
31 * COMBINATION WITH YOUR PRODUCTS.
32 *
33 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
35 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
37 * DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
38 * CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
39 * APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * @APPLE_DTS_LICENSE_HEADER_END@
42 */
43
44 #include <assert.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <string.h>
50 #include <sys/errno.h>
51
52 #include <dispatch/dispatch.h>
53
54
55 void readFileData(void* theSource) {
56 char buffer[10];
57 size_t estimated = dispatch_source_get_data(theSource);
58 printf("Estimated bytes available: %ld\n", estimated);
59 ssize_t actual = read(dispatch_source_get_handle(theSource), buffer, sizeof(buffer));
60 if (actual == -1) {
61 if (errno != EAGAIN) {
62 perror("read");
63 exit(-1);
64 }
65 } else {
66 if (estimated>actual) {
67 printf(" bytes read: %ld\n", actual);
68 } else {
69 // end of file has been reached.
70 printf(" last bytes read: %ld\n", actual);
71 dispatch_source_cancel(theSource);
72 }
73 }
74 }
75
76 void cancelSource(void* theSource) {
77 close(dispatch_source_get_handle(theSource));
78 dispatch_release(theSource);
79 dispatch_release(dispatch_get_current_queue());
80 printf("Everything is finished, goodbye.\n");
81 exit(0);
82 }
83
84 int main(int argc, char* argv[])
85 {
86 int infd;
87 dispatch_source_t fileSource;
88
89 if (argc != 2) {
90 fprintf(stderr, "usage: %s file ...\n", argv[0]);
91 exit(1);
92 }
93
94
95 infd = open(argv[1], O_RDONLY);
96 if (infd == -1) {
97 perror(argv[1]);
98 exit(1);
99 }
100
101 if (fcntl(infd, F_SETFL, O_NONBLOCK) != 0) {
102 perror(argv[1]);
103 exit(1);
104 }
105
106 fileSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, infd, 0, dispatch_queue_create("read source queue",NULL));
107 dispatch_source_set_event_handler_f( fileSource, readFileData);
108 dispatch_source_set_cancel_handler_f( fileSource, cancelSource);
109 // setting the context pointer to point to the source itself means the functions will get the source
110 // as a paremeter, from there they can get all the information they need.
111 dispatch_set_context(fileSource, fileSource);
112 dispatch_resume(fileSource);
113
114 dispatch_main();
115
116 return 0;
117 }